﻿using AnlianCommon.Common.Domains.BreedingManagement;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.SheepCommands.MeasurementBodyes
{
    public class MeasurementBodyQueryHandler : IRequestHandler<MeasurementBodyQueryCommand, ResultPage<MeasurementBody>>
    {
        private readonly IBaseRepository<MeasurementBody> _measurementBodyRepository;

        public MeasurementBodyQueryHandler(IBaseRepository<MeasurementBody> measurementBodyRepository)
        {
            _measurementBodyRepository = measurementBodyRepository;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<MeasurementBody>> Handle(MeasurementBodyQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<MeasurementBody>() { Code=(int)CommonErrorCode.Search};
            try
            {
                var measurementBody = await(from a in await _measurementBodyRepository
                                            .GetAll()
                                            select new MeasurementBody()
                                            {
                                                MeasurementBodyId = a.MeasurementBodyId,
                                                SheepNumber = a.SheepNumber,
                                                Variety = a.Variety,
                                                SheepSex = a.SheepSex,
                                                MeasurementHouse = a.MeasurementHouse,
                                                MeasurementField = a.MeasurementField,
                                                Measurementdate = a.Measurementdate,
                                                Measurementday = a.Measurementday,
                                                SheepHeight = a.SheepHeight,
                                                SheepLength = a.SheepLength,
                                                SheepChest = a.SheepChest,
                                                SheepPipe = a.SheepPipe,
                                                SheepScrotum = a.SheepScrotum,
                                                SheepBackfat = a.SheepBackfat,
                                                EyemuscleArea = a.EyemuscleArea,
                                                EyemuscleWidth = a.EyemuscleWidth,
                                                CreatedTime = a.CreatedTime,
                                                CreatedOwnerId = a.CreatedOwnerId,
                                            }) .ToListAsync();

                if (!string.IsNullOrEmpty(request.SheepNumber))
                {
                    measurementBody = measurementBody.Where(c => c.SheepNumber == request.SheepNumber).ToList();
                }
                if (!string.IsNullOrEmpty(request.Variety))
                {
                    measurementBody = measurementBody.Where(c => c.Variety == request.Variety).ToList();
                }
                if (request.SheepSex != null)
                {
                    measurementBody = measurementBody.Where(c => c.SheepSex == request.SheepSex).ToList();
                }
                result.TotalCount = measurementBody.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                measurementBody = measurementBody.OrderBy(c => c.MeasurementBodyId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = measurementBody;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
